//
//  LKSelectVideoAnnotationViewController.m
//  LKIntelligentVideoProject
//
//  Created by she1323 on 2018/11/7.
//  Copyright © 2018年 she1323. All rights reserved.
//

#import "LKSelectVideoAnnotationViewController.h"
#import <YWVideoPlayer/YWMediaPlayerView.h>
#import "AppDelegate.h"


@interface LKSelectVideoAnnotationViewController ()<YWMediaPlayerViewDelegate>

@property (nonatomic, strong)YWMediaPlayerView  *mediaPlayerView;

@property(nonatomic,strong)UIView *playerView;
@end

@implementation LKSelectVideoAnnotationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self testVideoPlayWithFatherView:self.view];
    // Do any additional setup after loading the view.
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    //确保在该控制器即将出现的时候开启全屏模式
    ((AppDelegate *) [[UIApplication sharedApplication] delegate]).fullScreen = YES;
    NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight];
    [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    // 确保在该控制器即将消失的时候关闭全屏模式
    ((AppDelegate *) [[UIApplication sharedApplication] delegate]).fullScreen = NO;
    NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
    [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
}

/**
 播放rtmp的视频播放源

 @param fatherView 所放父视图
 */
-(void)testVideoPlayWithFatherView:(UIView *)fatherView{
    self.playerView = [[UIView alloc] initWithFrame:CGRectMake(0, NAVIGATION_BAR_HEIGHT, YWSCREEN_WIDTH, YWMinPlayerHeight)];
    self.playerView.backgroundColor = [UIColor redColor];
    [fatherView addSubview:self.playerView];
    self.mediaPlayerView = [[YWMediaPlayerView alloc] init];
    [_mediaPlayerView playerViewWithUrl:@"rtmp://hzrtmp01.ys7.com:1935/livestream/624520592_1_1_1_0" WithTitle:@"" WithView:_playerView  WithDelegate:self];
    _mediaPlayerView.mediaControl.totalDurationLabel.hidden = YES;
    _mediaPlayerView.mediaControl.mediaProgressSlider.hidden = YES;
    _mediaPlayerView.mediaControl.currentTimeLabel.hidden = YES;
    _mediaPlayerView.delegate = self;
    _mediaPlayerView.isFullScreen = YES;
    [_mediaPlayerView.mediaControl.closeBtn addTarget:self action:@selector(backMap:) forControlEvents:UIControlEventTouchUpInside];
    _mediaPlayerView.mediaControl.fullScreenBtn.hidden = YES;
    __weak typeof(self) weakSelf = self;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 自动播放
        [weakSelf.mediaPlayerView.mediaControl playControl];
    });
}

/**
 重写SDK中返回按钮方法

 @param closeBtn 返回
 */
-(void)backMap:(UIButton *)closeBtn{
    [self.navigationController popViewControllerAnimated:YES];
}
/// 改变View大小布局
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
    
    if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft ||
        [UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeRight) {
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        _mediaPlayerView.frame = CGRectMake(0, 0, size.width,size.height);
        _mediaPlayerView.player.view.frame = CGRectMake(0, 0, size.width,size.height);
        _mediaPlayerView.mediaControl.fullScreenBtn.selected = YES;
        _mediaPlayerView.isFullScreen = YES;
        [window addSubview:_mediaPlayerView];
    } else {
        _mediaPlayerView.frame = CGRectMake(0, 0, size.width, size.width/16*9);
        _mediaPlayerView.player.view.frame = CGRectMake(0, 0, size.width, size.width/16*9);
        _mediaPlayerView.mediaControl.fullScreenBtn.selected = NO;
        _mediaPlayerView.isFullScreen = NO;
        [_playerView addSubview:_mediaPlayerView];
    }}
/// 点击关闭按钮
- (void)playerViewClosed:(YWMediaPlayerView *)player {
    
    NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
    [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
}

/// 全屏/非全屏切换
- (void)playerView:(YWMediaPlayerView *)player fullscreen:(BOOL)fullscreen {
    
    if (fullscreen == YES) {
        NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight];
        [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
    } else {
        NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
        [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
    }
}

/// 播放失败
- (void)playerViewFailePlay:(YWMediaPlayerView *)player {
    NSLog(@"播放失败");
}


/// 准备播放
- (BOOL)playerViewWillBeginPlay:(YWMediaPlayerView *)player {
    NSLog(@"准备播放");
    return YES;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
